package com.myt.matp.gui;

import com.myt.matp.core.Event;

import java.lang.Math;
import java.util.Enumeration;

/**
 * Describe class <code>UIColumn</code> here.
 *
 * @author <a href="mailto:emadridm@gmail.com">Enrique Madrid</a>
 * @version 1.0
 */
public class UIColumn extends UIContainer {

    protected UIColumn(int id, GUI gui) {	
	// Building itself
	super(id, gui);
    }

    public int getType() {
	return UIWidget.COLUMN;
    }

    protected void resized() {
	UIScrollBar sb;
	if (!hasVerticalScroll()) {
	    resizeChildrenToVerticalFix(getViewWidth(), getViewHeight());
	} else {
	    sb = getVerticalScrollBar();
	    sb.resize(getViewWidth(), getViewHeight());
	    resizeChildrenToVerticalScroll(getViewWidth(), getViewHeight());
	}
    }

    protected final void resizeChildrenToVerticalScroll(int w, int h) {
	Enumeration e;
	UIWidget uiw;

	e = getElements();
	while (e.hasMoreElements()) {
	    uiw = (UIWidget)e.nextElement();
	    uiw.resize(w, h/2);
	}
    }

    protected final void resizeChildrenToVerticalFix(int w, int h) {
	int nAutoH = 0;
	UIWidget uiw = null;
	int i;

        //#ifdef DEBUG_UICOLUMN
        //# System.out.println(getClass().getName() + ".resizeChildrenToVerticalFix() {");
        //#endif

        //#ifdef DEBUG_UICOLUMN
        //# System.out.println("\tAvaiable area");
        //# System.out.println("\tw = " + w);
        //# System.out.println("\th = " + h);
        //#endif

	// Establecer el area para los controles de dimensiones fijas (Ancho),
	// calcular la altura sobrante para controles autoexpandibles y
	// contar los controles de altura autoexpandibles.

	//#ifdef DEBUG_UICOLUMN
	//# System.out.println("\tSetting controls of fixed area");
	//#endif
	for (i = 0; i < getSize(); i++) {
	    uiw = elementAt(i);
	    if (uiw.getMaxHeight() != GUI.MAX_SIZE) {
		uiw.resize(w, h);
		//#ifdef DEBUG_UICOLUMN
		//# System.out.println("\t" + getClass().getName() + ".getHeight() = " + uiw.getHeight());
		//#endif
		h -= uiw.getHeight();
	    } else {
		nAutoH++;
	    }
	}

	// Establecer el area para los controles de dimesiones variables

	//#ifdef DEBUG_UICOLUMN
	//# System.out.println("\tSetting controls of variable area");
	//#endif
	if (nAutoH > 0) {
	    //#ifdef DEBUG_UICOLUMN
	    //# System.out.println("Avaiable area");
	    //# System.out.println("w = " + w);
	    //# System.out.println("h = " + h);
	    //#endif
	    for (i = 0; i < getSize(); i++) {
		uiw = elementAt(i);
		if (uiw.getMaxHeight() == GUI.MAX_SIZE) {
		    uiw.resize(w, h/nAutoH);
		}
		//#ifdef DEBUG_UICOLUMN
		//# System.out.println("\t" + getClass().getName() + ".getHeight() = " + uiw.getHeight());
		//#endif
	    }
	}

        //#ifdef DEBUG_UICOLUMN
        //# System.out.println("}");
        //#endif
    }

    protected int typeContained() {
	return UIWidget.UNKNOW;
    }

    protected int getContentHeight() {
	Enumeration e;
	UIWidget uiw;
	int ret = 0;

	e = getElements();
	while (e.hasMoreElements()) {
	    uiw = (UIWidget)e.nextElement();
	    ret += uiw.getHeight();
	}

	return ret;
    }

    private int calcSpacing() {
	int ret = 0;
	if (hasVerticalScroll() && (getSize() > 0)) {
	    ret = getViewHeight() - getContentHeight();
	    ret = (ret > 0) ? ret / getSize() : 0;
	}
	return ret;
    }

    protected void paintUIContainer(MGraphics g) {
	UIWidget uiw;
	int x0;
	int y0;
	int dh;
	int spacing;

        //#ifdef DEBUG_UICOLUMN
        //# System.out.println(getClass().getName() + ".paint() {");
        //#endif

	x0 = getViewX();
	y0 = getViewY();
	dh = 0;
	g.setClip(x0, y0, getViewWidth(), getViewHeight());
	spacing = calcSpacing();

	for (int i = 0; i < getRedisplay(); i++) {
	    uiw = elementAt(getFrame() + i);
	    //#ifdef DEBUG_UICOLUMN
	    //# System.out.println("\tpainting " + uiw.getClass().getName());
	    //# System.out.println("\t\tx0 = " + x0);
	    //# System.out.println("\t\ty0 = " + y0);
	    //# System.out.println("\t\tdh = " + dh);
	    //#endif
	    uiw.setPosX(x0);
	    uiw.setPosY(y0 + dh);
	    uiw.paint(g);
	    dh += (uiw.getHeight() + spacing);
	}

        //#ifdef DEBUG_UICOLUMN
        //# System.out.println("");
        //#endif
    }

    protected int computeFrame() {
	UIWidget uiw;
	int ret = 0;
	int dh = 0;
	int vh = 0;

        //#ifdef DEBUG_UICOLUMN
        //# System.out.println(getClass().getName() + ".computeFrame() {");
        //#endif

	uiw = getFocused();
	if (uiw != null) {
	    vh = getViewHeight();
	    ret = indexOf(uiw);
	    //#ifdef DEBUG_UICOLUMN
	    //# System.out.println("\tfocused = " + ret);
	    //#endif
	    dh = uiw.getHeight();
	    //#ifdef DEBUG_UICOLUMN
	    //# System.out.println("\t\tvh = " + vh);
	    //#endif
	    while (dh < vh) {
		if (ret == 0) {
		    break;
		}
		uiw = elementAt(--ret);
		dh += uiw.getHeight();
	    }
	    //#ifdef DEBUG_UICOLUMN
	    //# System.out.println("\t\tdh = " + dh);
	    //#endif
	    if (dh > vh) {
		ret++;
	    }
	}

	//#ifdef DEBUG_UICOLUMN
	//# System.out.println("\tcomputeFrame return " + ret);
	//#endif

        //#ifdef DEBUG_UICOLUMN
        //# System.out.println("}");
        //#endif

	return ret;
    }

    protected int computeRedisplay(int frame) {
	UIWidget uiw;
	int dh;
	int spacing;
	int ret = 0;

        //#ifdef DEBUG_UICOLUMN
        //# System.out.println(getClass().getName() + ".computeRedisplay() {");
        //#endif

	dh = 0;
	spacing = calcSpacing();
	for (int i = frame; i < getSize(); i++) {
	    uiw = elementAt(i);
	    dh += uiw.getHeight();
	    if (dh > getViewHeight()) {
		break;
	    }
	    ret++;
	}

        //#ifdef DEBUG_UICOLUMN
        //# System.out.println("\tret = " + ret);
        //#endif

        //#ifdef DEBUG_UICOLUMN
        //# System.out.println("}");
        //#endif

	return ret;
    }
}
